﻿using System;
using System.Collections.Generic;
using System.Text;
using Boomerang.Common.Configuration;
using Boomerang.Management.UIForms;

namespace Boomerang.Management.UIControlers
{
    public class OrganizationWizard
    {
        private const string Title = "Organization Configuration";
        private Organization Org { get; set; }
        private int formIndex = 0;

        private WizardFormBase[] forms;

        public OrganizationWizard() 
        {
            this.Org = new Organization();
            InitForms();
        }

        public OrganizationWizard(Organization org)
        {
            this.Org = org;
            InitForms();
        }

        private void InitForms()
        {
            forms = new WizardFormBase[3];
            forms[0] = new CrmConnectionForm(Title, WizardFormType.Start, Org);
            forms[1] = new BoomerangConnectionForm(Title, WizardFormType.Middle, Org);
            forms[2] = new CrmCustomizationsForm(Title, WizardFormType.Finish, Org);

            foreach (WizardFormBase item in forms)
            {
                item.OnWizardButtonClick += new WizardFormBase.ButtonEventHandler(item_OnWizardButtonClick);
                item.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            }
        }

        void item_OnWizardButtonClick(System.Windows.Forms.Form form, WizardButtonType buttonType)
        {
            switch (buttonType)
            {
                case WizardButtonType.Previous:
                    if (formIndex <= 0) throw new ApplicationException("Invalid try to go to Previous on Wizard");                    
                    forms[formIndex].Hide();
                    forms[--formIndex].Show();
                    forms[formIndex].BringToFront();
                    break;
                case WizardButtonType.Next:
                    if (formIndex >= forms.Length) throw new ApplicationException("Invalid try to go to Next on Wizard");
                    forms[formIndex].Hide();
                    forms[++formIndex].Show();
                    forms[formIndex].BringToFront();
                    break;
                case WizardButtonType.Finish:
                    if (formIndex != forms.Length-1) throw new ApplicationException("Invalid try to go to Finish on Wizard");
                    Finish();
                    break;
                case WizardButtonType.Cancel:                    
                    Close();
                    break;
                default:
                    break;
            }
            
        }

        public void Show()
        {
            forms[formIndex].Show();
        }

        private void  Finish()
        {
            ConfigurationManager.SetOrganization(Org);
            this.Close();
        }

        public void Close()
        {
            foreach (WizardFormBase form in forms)
                form.Close();
        }
        
    }


}
